package net.anzix.jimposition.transformations;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import javax.swing.JPanel;
import net.anzix.jimposition.*;

public class ResizeFilter extends BasicTransformation {

    float ratio = 0;

    static int allid = 0;

    private ResizeFilterPanel panel = new ResizeFilterPanel();

    private int id;

    public ResizeFilter() {
        id = allid++;
    }

    public byte[] alter(byte[] input) {
        try {
            PdfReader inputReader = new PdfReader(input);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int maxPageNum = inputReader.getNumberOfPages();
            Rectangle oldSize = inputReader.getPageSize(maxPageNum > 1 ? maxPageNum - 1 : 1);
            Rectangle newSize;
            String size = getAttribute("size").toString();
            if (size.equals("Custom")) {
                newSize = new Rectangle(oldSize.right() * ratio, oldSize.top() * ratio);
                if (ratio == 0) {
                    ratio = Integer.parseInt((String) getAttribute("ratio"));
                }
            } else {
                if (size.equals("A5")) {
                    newSize = PageSize.A5;
                } else if (size.equals("A1")) {
                    newSize = PageSize.A1;
                } else if (size.equals("A0")) {
                    newSize = PageSize.A0;
                } else if (size.equals("A3")) {
                    newSize = PageSize.A3;
                } else if (size.equals("A2")) {
                    newSize = PageSize.A2;
                } //A4 = default
                else {
                    newSize = PageSize.A4;
                }
                if (newSize.right() > newSize.top() & oldSize.right() < oldSize.top()) {
                    newSize.rotate();
                }
                if (newSize.right() / newSize.top() > oldSize.right() / oldSize.top()) {
                    ratio = newSize.top() / oldSize.top();
                } else {
                    ratio = newSize.right() / oldSize.right();

                }
            }
            Document document = new Document(newSize);
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            int pagenum = 1;

            PdfContentByte cb = writer.getDirectContent();
            for (int page = 1; page <= maxPageNum; page++) {
                document.newPage();
                cb.addTemplate(writer.getImportedPage(inputReader, page), ratio, 0f, 0f, ratio, 0f, 0f);
                cb.stroke();
            }
            document.close();
            return outputStream.toByteArray();
        } catch (DocumentException ex) {
            ex.printStackTrace();
            return null;
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public String toString() {
        return "Resize page";
    }

    public JPanel getPanel() {
        return panel;
    }

    public String getId() {
        return "resize" + id;
    }

    public void getPanelSettings() {
        setAttribute("ratio", panel.getRatioTextField().getText());
        setAttribute("size", panel.getSizeComboBox().getSelectedItem());
    }

    public void setPanelSettings() {
    }
}
